package com.sc.controller.patrol;

import com.sc.domain.patrol.PatrolPoint;
import com.sc.dto.PatrolPointDTO;
import com.sc.result.Result;
import com.sc.service.patrol.PatrolPointService;
import com.sc.vo.PatrolPointVO;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 巡更点管理
 * @author:Zuiliu
 * 功能：增删改查、作列表展示
 * 数据格式
 */
@CrossOrigin
@RestController
@RequestMapping("/patrolPoint")
@RequiredArgsConstructor
public class PatrolPointController {

    private final PatrolPointService ivrs;

    // 添加巡更点
    @PostMapping("/addPatrolPoint")
    public Result addPatrolPoint(@RequestBody PatrolPoint patrolPoint) {
        patrolPoint.setAddTime(LocalDateTime.now());
        try {
            return Result.success("添加成功", ivrs.save(patrolPoint));
        } catch (DuplicateKeyException e) {
            return Result.error("巡更点名称重复，换一个试试");
        }
    }

    // 删除巡更点
    @DeleteMapping("/deletePatrolPoint")
    public Result deletePatrolPoint(Integer id) {
        return Result.success("删除成功", ivrs.removeById(id));
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        return Result.success("删除成功", ivrs.removeByIds(ids));
    }

    // 修改巡更点
    @PutMapping("/updatePatrolPoint")
    public Result updatePatrolPoint(@RequestBody PatrolPoint patrolPoint) {
        try {
            return Result.success("修改成功", ivrs.updateById(patrolPoint));
        } catch (DuplicateKeyException e) {
            return Result.error("巡更点名称重复，换一个试试");
        }
    }

    // 查询巡更点
    @GetMapping("/selectPatrolPoint")
    public Result selectPatrolPoint(Integer id) {
        return Result.success(ivrs.getById(id));
    }

    // 列表展示
    @PostMapping("/listPatrolPoint")
    public Result listPatrolPoint(@RequestBody PatrolPointDTO patrolPointDTO) {
        return Result.success(ivrs.pageQuery(patrolPointDTO));
    }

    @GetMapping("/getAll")
    public Result<List<PatrolPoint>> getAll() {
        return Result.success(ivrs.list());
    }
}
